using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Text;
using System.Collections.Generic;

/// <summary>
/// Summary description for utility
/// </summary>
public static class Utility
{
    public static void CopyListItem(ListItemCollection source, ListItemCollection destination)
    {
        destination.Clear();
        foreach (ListItem item in source)
        {
            destination.Add(item);
        }
    }

    public static void SetSelectedItem(ListControl control, string valueToSelect)
    {
        for (int i = 0; i < control.Items.Count; i++)
        {
            if (control.Items[i].Value == valueToSelect)
            {
                control.SelectedIndex = i;
                return;
            }
        }
        throw new IndexOutOfRangeException("The value to select was not found in the ListItems!");
    }

    public static string GetLanguageUrl(string lang)
    {
        HttpContext context=HttpContext.Current;
        NameValueCollection newQuery = new NameValueCollection();

        foreach (string key in context.Request.QueryString.AllKeys)
        {
            if (key != "lang")
                newQuery.Add(key, context.Request.QueryString[key]);
        }
        newQuery.Add("lang", lang);

        string newQueryString = "?";
        foreach (string key in newQuery)
        {
            newQueryString = newQueryString + key + "=" + newQuery[key] + "&";
        }
        
        newQueryString = newQueryString.Substring(0, newQueryString.Length - 1);

        return (context.Request.Path + newQueryString);
    }

    public static string GetUrl(string view, params string[] queryParameters)
    {
        StringBuilder urlBuilder = new StringBuilder("~/");
        urlBuilder.Append(view);
        urlBuilder.Append(".aspx?");
        foreach (string queryParameter in queryParameters)
        {
            urlBuilder.Append(queryParameter);
            urlBuilder.Append("&");
        }
        urlBuilder.Append("lang=");
        urlBuilder.Append(UserContext.Language);
        return urlBuilder.ToString();

    }



   
}
